{# SPDX-License-Identifier: Apache-2.0 -#}
{% extends "manage_organization_base.html" %}
{% set user = request.user %}
{% set title = gettext("Organization projects") %}
{% set active_tab = "projects" %}
{% block title %}
  {% trans organization_name=organization.name %}Manage '{{ organization_name }}' projects{% endtrans %}
{% endblock %}
{% block main %}
  <h2>
    {% trans %}Projects{% endtrans %}
    <span class="badge badge--neutral">{{ active_projects|length }}</span>
  </h2>
  <div class="package-list">
    {% set user_has_two_factor = request.user.has_two_factor %}
    {% for project in active_projects %}
      {% set release = project.releases[0] if project.releases else None %}
      <div class="package-snippet">
        <div class="split-layout split-layout--no-wrap split-layout--wrap-on-tablet">
          <div>
            <h3 class="package-snippet__title">
              {{ project.name }}
              {% if project.organization %}
                <a href="{{ request.route_path('manage.organization.projects', organization_name=project.organization.normalized_name) }}"
                   class="badge">
                  <i class="fa fa-sitemap" aria-hidden="true"></i>{{ project.organization.name }}
                </a>
              {% endif %}
              {% if project.name in projects_sole_owned %}
                <a href="{{ request.route_path('manage.project.roles', project_name=project.normalized_name) }}"
                   class="badge badge--warning">{% trans %}Sole owner{% endtrans %}</a>
              {% endif %}
            </h3>
            {% if release %}
              <p class="package-snippet__meta">
                {% trans release_date=humanize(release.created) %}Last released {{ release_date }}{% endtrans %}
              </p>
              <p class="package-snippet__description">{{ release.summary }}</p>
            {% else %}
              <p class="package-snippet__meta">
                {% trans creation_date=humanize(project.created) %}Created {{ creation_date }}{% endtrans %}
              </p>
            {% endif %}
          </div>
          <div class="package-snippet__buttons">
            {% if project.name in projects_owned %}
              {# Show manage button #}
              <a href="{{ request.route_path('manage.project.settings', project_name=project.normalized_name) }}"
                 class="button button--primary"
                 title="{% trans %}Manage this project{% endtrans %}">{% trans %}Manage{% endtrans %}</a>
            {% else %}
              {# Show disabled button #}
              <button class="button button--primary"
                      disabled
                      title="{% trans %}You are not an owner of this project{% endtrans %}">
                {% trans %}Manage{% endtrans %}
              </button>
            {% endif %}
            {% if project.releases %}
              <a href="{{ request.route_path('packaging.project', name=project.normalized_name) }}"
                 class="button"
                 title="{% trans %}View this project's public page{% endtrans %}">{% trans %}View{% endtrans %}</a>
            {% else %}
              <button class="button"
                      disabled
                      title="{% trans %}This project has no releases{% endtrans %}">
                {% trans %}View{% endtrans %}
              </button>
            {% endif %}
          </div>
        </div>
      </div>
    {% else %}
      <div class="callout-block no-top-margin">
        <p>
          {% trans %}There are no projects in your organization, yet.{% endtrans %}
          {% if request.has_permission(Permissions.OrganizationProjectsAdd) %}
            {%+ trans href='https://packaging.python.org/' %}Get started by adding a project that you own using the form below. To learn how to create a new project, visit the <a href="{{ href }}" target="_blank" rel="noopener">Python Packaging User Guide</a>{% endtrans %}
          {% endif %}
        </p>
      </div>
    {% endfor %}
  </div>
  {% if request.has_permission(Permissions.OrganizationProjectsAdd) %}
    <hr>
    {{ form_error_anchor(add_organization_project_form) }}
    <section id="add-organization-project">
      <h2 class="sub-title">{% trans %}Add project to organization{% endtrans %}</h2>
      <form class="radio-toggle-form" method="post">
        <input name="csrf_token"
               type="hidden"
               value="{{ request.session.get_csrf_token() }}">
        {{ form_errors(add_organization_project_form) }}
        {# New or existing? radio buttons should be siblings of div.form-group below. #}
        <label class="form-group__label" for="add_existing_project">{% trans %}️New or existing project?{% endtrans %}</label>
        {% for subfield in add_organization_project_form.add_existing_project %}
          {{ subfield(data_target=".radio-toggle-" + loop.index|string) }}
          {{ subfield.label }}
        {% endfor %}
        <div id="add-existing-project-errors">{{ field_errors(add_organization_project_form.add_existing_project) }}</div>
        <p class="form-group__help-text">
          {% trans %}Transfer an existing project that you own, or create and add a new project to the organization.{% endtrans %}
        </p>
        <p class="form-group__text"></p>
        {# end radio buttons #}
        <div class="form-group radio-toggle radio-toggle-1">
          <label class="form-group__label" for="project">
            {% trans %}️Name of existing project{% endtrans %}
            <span class="form-group__required">{% trans %}(required){% endtrans %}</span>
          </label>
          {% set no_existing_projects = add_organization_project_form.existing_project_name.choices|length <= 1 %}
          {{ add_organization_project_form.existing_project_name(class_="form-group__field",
                    disabled=no_existing_projects,
                    title=gettext("You do not own any projects that you can add to this organization.") if no_existing_projects else "",
          aria_describedby="existing-project-name-errors",
          ) }}
          <div id="existing-project-name-errors">{{ field_errors(add_organization_project_form.existing_project_name) }}</div>
          <p class="form-group__help-text">
            {% trans %}
            Owners or managers of this organization can transfer a project that they own to this organization if the project has not been added to another organization. A project that has been added to another organization can be transferred from the project settings page.
          {% endtrans %}
        </p>
      </div>
      <input value="{% trans %}Transfer existing project{% endtrans %}"
             class="button button--primary radio-toggle radio-toggle-1"
             type="submit">
      <div class="form-group radio-toggle radio-toggle-2">
        <label class="form-group__label" for="project">
          {% trans %}️Name of new project{% endtrans %}
          <span class="form-group__required">{% trans %}(required){% endtrans %}</span>
        </label>
        {{ add_organization_project_form.new_project_name(class_="form-group__field",
                aria_describedby="new-project-name-errors",) }}
        <div id="new-project-name-errors">{{ field_errors(add_organization_project_form.new_project_name) }}</div>
        <p class="form-group__help-text">
          {% trans %}
          Owners of this organization can create an empty project and add it to the organization.
        {% endtrans %}
      </p>
    </div>
    <input value="{% trans %}Create and add new project{% endtrans %}"
           class="button button--primary radio-toggle radio-toggle-2"
           type="submit">
  </form>
</section>
{% endif %}
{% endblock %}
